package com.kly.user.service.traffic;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.StringUtils;
import com.kly.user.config.NacosValueConfig;
import com.kly.user.db.dao.CustomersDao;
import com.kly.user.db.dao.UserAppsflyerFromAppDao;
import com.kly.user.db.dao.UserAppsflyerReportDetailDao;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.entity.CustomersEntity;
import com.kly.user.db.entity.UserAppsflyerFromAppEntity;
import com.kly.user.db.entity.UserAppsflyerReportDetailEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.dto.TrafficSourceDto;
import com.kly.user.dto.vo.TrafficSourceReq;
import com.kly.user.enums.AppCodeEnum;
import com.kly.user.enums.TrafficSourceEnum;
import com.kly.user.service.WhiteListService;
import com.kly.user.serviceImpl.UserTrafficSourceServiceImpl;
import com.kly.user.serviceImpl.dto.TrafficSourceEnableSwitch;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("ppTrafficFacadeService")
public class PPTrafficFacadeServiceImpl implements TrafficFacadeService {



    @Resource
    NacosValueConfig nacosValueConfig;

    @Resource
    UserAppsflyerFromAppDao userAppsflyerFromAppDao;
    @Resource
    UserAppsflyerReportDetailDao userAppsflyerReportDetailDao;
    @Resource
    WhiteListService whiteListService;
    @Resource
    UserDao userDao;
    @Resource
    CustomersDao customersDao;


    @PostConstruct
    public void setUp() {
        UserTrafficSourceServiceImpl.trafficFacadeService.put(AppCodeEnum.PP.getAppCode(), this);
    }

    @Override
    public Response<TrafficSourceDto> getTrafficSource(TrafficSourceReq req, String appCode) {
        // 获取白名单配置
        final Boolean whiteUserFlag = whiteListService.in(req.getAppCode(), req.getUserCode(), req.getMobile(), req.getEmail()).getData();

        // 白名单用户
        if (whiteUserFlag) {
            return Response.ok(TrafficSourceDto.toDto(null, TrafficSourceEnum.Organic, Boolean.TRUE));
        }

        // 当开关状态为[关闭]时，账号注册进来，全部走非自然流量展示，以及对应的流量逻辑
        // 总开关配置
        final Integer trafficControlEnableSwitch = getTrafficControlEnableSwitch(req.getAppCode()).getData();
        if (trafficControlEnableSwitch == 0) {
            return Response.ok(TrafficSourceDto.toDto(trafficControlEnableSwitch, TrafficSourceEnum.None_Organic, Boolean.FALSE));
        }

        final UserEntity user = userDao.selectByUserCode(req.getUserCode());
        final CustomersEntity customer = customersDao.getCustomer(user.getMobile(), user.getEmail(), null);
        if (customer != null) {
            if (customer.getTrafficType() != null && customer.getTrafficType() == TrafficSourceEnum.None_Organic.getType()) {
                return Response.ok(TrafficSourceDto.toDto(trafficControlEnableSwitch, TrafficSourceEnum.None_Organic, Boolean.FALSE));
            }
            return Response.ok(TrafficSourceDto.toDto(trafficControlEnableSwitch, TrafficSourceEnum.Organic, Boolean.FALSE));
        }

        return Response.ok(TrafficSourceDto.toDto(trafficControlEnableSwitch, TrafficSourceEnum.Organic, Boolean.FALSE));
    }


    public Response<Integer> getTrafficControlEnableSwitch(String appCode) {
        if (StringUtils.isEmpty(appCode)) {
            return Response.error("appCode should not be empty");
        }

        final List<TrafficSourceEnableSwitch> trafficSourceList = JSONObject.parseArray(nacosValueConfig.getTrafficSourceEnableSwitch(), TrafficSourceEnableSwitch.class);
        final Integer enableSwitch = trafficSourceList.parallelStream().filter(o -> o.getAppCode().equals(appCode)).findFirst()
                .map(TrafficSourceEnableSwitch::getTrafficSourceEnableSwitch).orElse(2);
        return Response.ok(enableSwitch);

    }
}
